const {resolve}=require('path');
const files=new Promise(resolve1 => {
	require('fs').readdir(resolve(__dirname,'./script_src/html'), (err,f)=> {
		resolve1(f[f.length-1]);
	});
})
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const reload = browserSync.reload;
const delHandler = () => {
	return require('del')(["./dist"])
};

const assetHandler = () => {
	return gulp.src('./script_src/assets/**/*')
		.pipe(gulp.dest('./dist/assets/'))
}
const htmlMinHandler = async () => {
	let target= await files;
	return gulp.src(`./script_src/html/${target}/*`)
		.pipe(gulp.dest('./dist/pages/'))
		.pipe(reload({stream: true}));
}
const watchHandler = async () => {
	let target= await files;
	browserSync.init({
		server: "./dist",
		browser: "google chrome",
		startPath:encodeURI(`pages/index.html`)
	});
	gulp.watch(`./script_src/html/${target}/*`, htmlMinHandler).on('change', reload);
	gulp.watch('./script_src/assets/**/*', assetHandler);
}
module.exports.default = gulp.series(
	delHandler,
	gulp.parallel(assetHandler, htmlMinHandler),
	watchHandler,
);
